package com.haierCamera.customapplication.ui.main;

import android.content.Intent;
import android.os.Bundle;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.ApiService;
import com.haierCamera.customapplication.databinding.HzklActivityNameEditBinding;
import com.haierCamera.customapplication.utils.ResourceConvertUtils;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import androidx.databinding.DataBindingUtil;
import dagger.Lazy;

public class HZKLEditCameraNameActivity extends BaseActivity {

    HzklActivityNameEditBinding binding;

    @Inject
    ApiService apiService;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_name_edit);
        binding.imgReturn.setOnClickListener((v)->{
            finish();
        });

        binding.edName.setText(getIntent().getStringExtra("name"));
        binding.tvConfirm.setOnClickListener((v)->{
            if (binding.edName.getText().toString().equals("")){
                toast("请输入名称");
                return;
            }
            modifyName(Long.parseLong(getIntent().getStringExtra("accountDeviceId"))
                    ,binding.edName.getText().toString());
        });
    }

    public void modifyName(long accountDeviceId,String name) {
        ResourceConvertUtils.convertToResource(apiService.modifyame(accountDeviceId,name))
                .observe(this, resource -> {
                    assert resource != null;
                    switch (resource.status) {
                        case ERROR:
                            errorProcessLazy.get().process(resource);

                            break;
                        case SUCCESS:

                            Bundle b = new Bundle();
                            Intent intent = new Intent();
                            b.putString("name",name);
                            intent.putExtras(b);
                            setResult(RESULT_OK,intent);
                            finish();

                            break;
                        case LOADING:
                            break;

                    }
                });
    }
}
